<form class="ruleForm" data-toggle="validate">
    <table class="ruletable editForm" width="100%"></table>
</form>

<script>
    $(function () {
        var this_id = Cache.get(Module + 'GridIndex');
        var ModuleInfo = Cache.get('ModuleInfo');

        Qrck.get('/admin/rule/tree', {}, '', function (ruleLists) {


            Qrck.get('/admin/' + ModuleInfo.Module + '/rulelsit?id=' + this_id, {}, '', function (checkd) {

                $('.ruletable').append(eachTableTree(ruleLists, checkd));

            }, function () {
                $('.ruletable').append(eachTableTree(ruleLists, checkd));
            });
        });

        function eachTableTree(ruleLists,checkd) {
            var html = '';
            $.each(ruleLists, function (index, value) {

                html += '<tr>';
                html += '<td>'+ input(value, checkd)  + value.title + '</td>';
                html += '<td>';
                if (typeof value.children !== 'undefined') {
                    html += '<table>';
                    html += eachTableTree(value.children);
                    html += '</table>';

                }
                html += '</td>';
                html += '</tr>';


            });
            return html;
        }




        function eachTableForm(ruleLists, checkd) {
            var html = '';
            html += '<table class="editForm">';
            $.each(ruleLists, function (key, value) {
                html += '<tr>';

                html += '<td>' + input(value, checkd) + value.title + '</td>';
                if (typeof value.children !== 'undefined') {
                    html += '<td>';
                    html += '<table class="editForm" >';
                    $.each(value.children, function (k, v) {
                        html += '<tr>';
                        html += '<td>' + input(v, checkd) + v.title + '</td>';
                        html += '<td>';
                        if (typeof v.children !== 'undefined') {
                            // html += '<table>';
                            $.each(v.children, function (kk, vv) {
                                // html += '<tr>';
                                html += input(vv, checkd) + vv.title;
                            });
                        }
                        html += '</td>';
                        html += '</tr>';
                    });
                    html += '</table>';
                    html += '</td>';
                }
                html += '</tr>';
            });
            html += '</table>';
            return html;
        }

        function input(value, checkd) {
            // if (in_array(checkd, value.id)) {
            //     return '<input name="r' + value.id + '"  checked="checked" value="1"  type="checkbox" style="margin-left:15px; margin-right: 5px"  >';
            // }
            return '<input name="r' + value.id + '"  type="checkbox" value="0" style="margin-left:15px; margin-right: 5px" >';
        }

    });
</script>